import 'package:flash_chat_flutter/constants.dart';
import 'package:flash_chat_flutter/screens/chat_screen.dart';
import 'package:flash_chat_flutter/services/networking.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:modal_progress_hud_nsn/modal_progress_hud_nsn.dart';
import '../components/rounded_button.dart';
import 'login_screen.dart';

class RegistrationScreen extends StatefulWidget {
  static const String id = 'registration_screen';

  @override
  _RegistrationScreenState createState() => _RegistrationScreenState();
}

Future getRegister({required String email, required String password}) {
  NetworkHelper networkHelper = NetworkHelper();
  return networkHelper.register(email: email, password: password);
}

class _RegistrationScreenState extends State<RegistrationScreen> {
  late String email;
  late String password;
  late bool showSpinner = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ModalProgressHUD(
        inAsyncCall: showSpinner,
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Flexible(
                child: Hero(
                  tag: 'logo',
                  child: Container(
                    height: 200.0,
                    child: Image.asset('images/logo.png'),
                  ),
                ),
              ),
              SizedBox(height: 48.0),
              TextField(
                keyboardType: TextInputType.emailAddress,
                textAlign: TextAlign.center, //设置文字据中
                onChanged: (value) {
                  email = value;
                },
                decoration: kTextFieldDecoration.copyWith(
                  hintText: 'Enter your email',
                ),
              ),
              SizedBox(height: 8.0),
              TextField(
                obscureText: true, //设置隐藏密码
                textAlign: TextAlign.center,
                onChanged: (value) {
                  password = value;
                },
                decoration: kTextFieldDecoration.copyWith(
                  hintText: 'Enter your password',
                ),
              ),
              SizedBox(height: 24.0),
              RoundedButton(
                color: Colors.blueAccent,
                txt: 'Register',
                onPressed: () async {
                  print('email：$email');
                  print('password：$password');
                  setState(() {
                    showSpinner = true;
                  });
                  dynamic register = await getRegister(
                    email: email,
                    password: password,
                  );
                  if (register['code'] == 200) {
                    Navigator.pushNamed(
                      context,
                      ChatScreen.id,
                      arguments: {"email": email},
                    );
                    setState(() {
                      showSpinner = false;
                    });
                  } else {}
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
